<% unless @saved -%>
  TracksPages.show_errors(html_for_error_messages());

  function html_for_error_messages() {
    return "<%= js_error_messages_for(@todo) %>";
  }
<% else

  animation = []
  if should_show_new_item
    if should_add_new_container
      animation << "insert_new_container_with_new_todo";
    else
      animation << "add_todo_to_existing_container";
    end
  end
  animation << "remove_empty_message_container";
  animation << "update_predecessors";
  animation << "clear_form";
-%>

  <%= render_animation(animation) %>
  TracksPages.page_inform("<%=escape_javascript @status_message%>");
  TracksPages.hide_errors();
  TracksPages.set_page_badge(<%= @down_count %>);

  function clear_form(next_steps) {
    $('#todo-form-new-action').clearForm();
    $('#todo-form-new-action').clearDeps();
    TracksForm.set_context_name('<%=escape_javascript @initial_context_name%>');
    TracksForm.set_project_name_and_default_project_name('<%=escape_javascript @initial_project_name%>');
    TracksForm.set_tag_list_and_default_tag_list('<%=escape_javascript @initial_tags%>');
    $('#todo-form-new-action input:text:first').focus();
    $('#new_todo_starred_link .todo_star').removeClass('starred');
    $('#new_todo_starred').val('false');
    next_steps.go();
  }

  function insert_new_container_with_new_todo(next_steps) {
    <%-
      empty_id = '#no_todos_in_view'
      empty_id = '#deferred_pending_container-empty-d' if source_view_is :tickler
    -%>
    $('<%=empty_id%>').slideUp(100, function() {
      $('#display_box').prepend(html_for_new_container());
      next_steps.go();
    });
  }

  function add_todo_to_existing_container(next_steps) {
    $('#<%= empty_container_msg_div_id %>').hide();
    var container = $('#<%= item_container_id(@todo) %>_items');
    container.append(html_for_new_todo());
    TracksPages.sort_container(container);

    $('#<%= item_container_id(@todo) %>').slideDown(500, function() {
      $('#<%= dom_id(@todo) %>').effect('highlight', {}, 2000 );
      next_steps.go();
    });
  }

  function remove_empty_message_container(next_steps) {
    $('#no_todos_in_view').slideUp(100, function() { });
    next_steps.go();
  }

  function update_predecessors(next_steps) {
    <%  @todo.uncompleted_predecessors.each do |p| -%>
      if ($('<%=item_container_id(p)%>')) {
        $('#<%=dom_id(p)%>').html('<%= js_render(p, { :parent_container_type => parent_container_type, :source_view => @source_view })%>');
      }
    <% end -%>
    next_steps.go();
  }

  function html_for_new_container() {
    <%
      want_render = @group_view_by == 'project' ? @new_project_created : @new_context_created
      container   = @group_view_by == 'project' ? @todo.project        : @todo.context
    %>
    return "<%= want_render ? js_render(container, { :settings => {:collapsible => true} }) : "" %>";
  }

  function html_for_new_todo() {
    return "<%= @saved ? js_render(@todo, { :parent_container_type => parent_container_type, :source_view => @source_view }) : "" %>";
  }

<% end -%>
